package com.itheima.util;

import com.itheima.model.ResponseResult;
import com.itheima.model.ResultCodeEnum;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @作者 itcast
 * @创建日期 2021/3/8 16:30
 **/
public class ResponseUtils {
    public static void okResult(HttpServletResponse response,Object obj){
        response = setResponse(response);
        try {
            response.getWriter().write(JsonUtils.objToJson(ResponseResult.okResult(obj)));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("返回响应Response失败");
        }
    }
    public static void okResult(HttpServletResponse response){
        response = setResponse(response);
        try {
            response.getWriter().write(JsonUtils.objToJson(ResponseResult.okResult()));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("返回响应Response失败");
        }
    }
    public static void noLoginResult(HttpServletResponse response){
        response = setResponse(response);
        try {
            response.getWriter().write(JsonUtils.objToJson(ResponseResult.errorResult(ResultCodeEnum.NEED_LOGIN)));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("返回响应Response失败");
        }
    }
    public static void noLoginResult(HttpServletResponse response,String msg){
        response = setResponse(response);
        try {
            response.getWriter().write(JsonUtils.objToJson(ResponseResult.errorResult(ResultCodeEnum.NEED_LOGIN,msg)));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("返回响应Response失败");
        }
    }
    public static void errorResult(HttpServletResponse response,String msg){
        response = setResponse(response);
        try {
            response.getWriter().write(JsonUtils.objToJson(ResponseResult.errorResult(ResultCodeEnum.SERVER_FAIL,msg)));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("返回响应Response失败");
        }
    }
    public static void errorResult(HttpServletResponse response){
        response = setResponse(response);
        try {
            response.getWriter().write(JsonUtils.objToJson(ResponseResult.errorResult(ResultCodeEnum.SERVER_FAIL)));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("返回响应Response失败");
        }
    }
    /**
     * 设置响应内容
     * @param response
     * @return
     */
    private static HttpServletResponse setResponse(HttpServletResponse response) {
        response.setStatus(200); // 设置状态码
        response.setContentType("application/json;charset=utf8");// 设置返回类型
        // 设置允许跨域
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        return response;
    }
}
